import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

// 1. 配置路由
const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    component: () => import("@/pages/LoGin/loginview.vue"),
  },
  {
    path: "/register",
    component: () => import("@/pages/ReGister/register.vue"),
  },
  {
    path: "/index",
    component: () => import("@/pages/InDex/Index.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/pages/Home/HomeView.vue"),
        meta:{
          title:'首页',
          isShow:false
        }
      },
      {
        path: "/ScenerySpot",
        name:'ScenerySpot',
        component: () => import("@/pages/ScenerySpot/ScenerySpot.vue"),
        meta:{
          title:'景区',
          isShow:false
        }
      },
      {
        path: "/SceneryPoint",
        name: "SceneryPoint",
        component: () => import("@/pages/SceneryPoint/SceneryPoint.vue"),
        meta:{
          title:'景点分析',
          isShow:false
        }
      },
      {
        path: "/ReView",
        name: "ReView",
        component: () => import("@/pages/ReView/ReView.vue"),
        meta:{
          title:'评论分析',
          isShow:false
        }
      },
      {
        path: "/DiaLogue",
        name: "DiaLogue",
        component: () => import("@/pages/DiaLogue/DiaLogue.vue"),
        meta:{
          title:'功能模块',
          isShow:false
        }
      },
      {
        path: "/EditUser",
        component: () => import("@/pages/EditUser/EditUser.vue"),
        meta:{
          title:'首页2',
          isShow:true
        }
      },
    ]
  },
  {
    path: "/ToInfogrAphics",
    component: () => import("@/pages/ToInfogrAphics/ToInfogrAphics.vue"),
  },
  {
    path: "/ProvinceAphics",
    component: () => import("@/pages/ProvinceAphics/ProvinceAphics.vue"),
  },
  {
    path: '/',
    redirect: '/login'
  },
];
// 2.返回一个 router 实列，为函数，配置 history 模式
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// import { menuStore } from "@/store/modules/menu";
// const menulist = menuStore();
// let { login_user } = storeToRefs(menulist);
router.beforeEach(async (to, from, next) => {
  let menu = localStorage.getItem("menu");
  let menus:any = null
  if (menu) {
    menu = JSON.parse(menu);
    // console.log(menu);
    if(menu.login_user){
      next()
    }
  }else{
    if (to.path == "/login" || to.path == "/register") {
      next();
      return;
    }
    ElMessage({
      message: "您当前未登录,请进行登录",
      type: "warning",
    });
    next("/login");
  }
  // if (menu.login_user) {


    // 未登录
    // if (JSON.parse(localStorage.getItem('menu'))) {
      // next();
    // }
  // }
});


// 3.导出路由   去 main.ts 注册 router.ts
export default router